<?php
namespace App\Http\Controllers\Laifei\Work;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class LaifeiWorkHangUpController extends Controller
{

    public function index()
    {

        $all = $this->all;
        $pageSize = $all['pageSize'] ?? 20;
        $pageNo = $all['pageNo'] ?? 1;
        try{

            $where = function ($query) use ($all) {
                if(isset($all['is_delete'])){
                    $query->where('ehu.is_delete','=',$all['is_delete']);
                }
                if(isset($all['work_no'])){
                    $query->where('ew.work_no','=',$all['work_no']);
                }
                if(isset($all['shop_name'])){
                    $query->where('shop.shop_name','like','%'.$all['shop_name'].'%');
                }
                if(isset($all['talent_name'])){
                    $query->where('talent.account_name','like','%'.$all['talent_name'].'%');
                }
            };

            $total = DB::table('explore_hang_up as ehu')
                ->leftJoin('explore_work as ew','ehu.work_id','=','ew.work_id')
                ->leftJoin('shop','ew.shop_id','=','shop.shop_id')
                ->leftJoin('talent','ew.talent_id','=','talent.talent_id')
                ->where($where)
                ->count();

            $info = DB::table('explore_hang_up as ehu')
                ->leftJoin('explore_work as ew','ehu.work_id','=','ew.work_id')
                ->leftJoin('shop','ew.shop_id','=','shop.shop_id')
                ->leftJoin('talent','ew.talent_id','=','talent.talent_id')
                ->where($where)
                ->select('ehu.hang_id','ehu.work_id','ehu.refused_at','ehu.is_delete','ehu.feedback','ehu.feedback_at','ehu.created_at',
                    'ew.work_no','shop.shop_name','talent.account_name')
                ->orderByDesc('ehu.created_at')
                ->skip(($pageNo - 1) * $pageSize)
                ->take($pageSize)
                ->get()
                ->map(function($item){
                    $item->status = $item->is_delete?'已关闭':'进行中';
                    switch ($item->feedback){
                        case 1:
                            $item->feedback_name = '申诉失败';
                            break;
                        case 2:
                            $item->feedback_name = '申诉成功';
                            break;
                        default:
                            $item->feedback_name = '';
                    }
                    return $item;
                })
                ->toArray();

            return $this->setReturnJson(0,'success',['total'=>$total,'info'=>$info]);

        }catch (\Exception $e){
            Log::error(LaifeiWorkHangUpController::class,['msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
            return $this->setReturnJson(-1,'查询失败',['msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
        }

    }

}
